﻿using System.Collections.Specialized;
using AT.IBMS.Framework.Data;
using System.Data.Common;
using System;
using System.Data;

namespace AT.IBMS.Framework.Logging
{
    public class WriteLogToDatabaseProvider : WriteLogProviderBase
    {
        private string connectionStringName;
        private string procedureName;

        public override void Initialize(string name, NameValueCollection config)
        {

            connectionStringName = config["connectionStringName"];
            procedureName = config["writeLogStoredProcedureName"];

            if (string.IsNullOrEmpty(procedureName))
            {
                throw new ArgumentNullException("writeLogStoredProcedureName");
            }

        }

        internal string ConnectionStringName
        {
            get
            {
                return connectionStringName;
            }
        }

        public override void Write(LogEntry log)
        {

            DataProvider provider;
            DbParameter id;
            DbParameter message;
            DbParameter title;
            DbParameter categories;
            DbParameter priority;
            DbParameter eventId;
            DbParameter severity;
            DbParameter machineName;
            DbParameter timeStamp;
            DbParameter extendedProperties;
            DbParameter appDomainName;
            DbParameter processId;
            DbParameter processName;
            DbParameter managedThreadName;

            provider = FrameworkManager.CreateDataProvider(connectionStringName);
            id = provider.CreateParameter("@id", log.Id);

            if (string.IsNullOrEmpty(log.Message))
            {
                message = provider.CreateParameter("@message", DBNull.Value);
            }
            else
            {
                message = provider.CreateParameter("@message", log.Message);
            }

            if (string.IsNullOrEmpty(log.Title))
            {
                title = provider.CreateParameter("@title", DBNull.Value);
            }
            else
            {
                title = provider.CreateParameter("@title", log.Title);
            }

            if (string.IsNullOrEmpty(log.MachineName))
            {
                machineName = provider.CreateParameter("@machineName", DBNull.Value);
            }
            else
            {
                machineName = provider.CreateParameter("@machineName", log.MachineName);
            }

            if (string.IsNullOrEmpty(log.AppDomainName))
            {
                appDomainName = provider.CreateParameter("@appDomainName", DBNull.Value);
            }
            else
            {
                appDomainName = provider.CreateParameter("@appDomainName", log.AppDomainName);
            }

            if (string.IsNullOrEmpty(log.ProcessId))
            {
                processId = provider.CreateParameter("@processId", DBNull.Value);
            }
            else
            {
                processId = provider.CreateParameter("@processId", log.ProcessId);
            }

            if (string.IsNullOrEmpty(log.ProcessName))
            {
                processName = provider.CreateParameter("@processName", DBNull.Value);
            }
            else
            {
                processName = provider.CreateParameter("@processName", log.ProcessName);
            }

            if (string.IsNullOrEmpty(log.ManagedThreadName))
            {
                managedThreadName = provider.CreateParameter("@managedThreadName", DBNull.Value);
            }
            else
            {
                managedThreadName = provider.CreateParameter("@managedThreadName", log.ManagedThreadName);
            }

            priority = provider.CreateParameter("@priority", log.Priority);
            eventId = provider.CreateParameter("@eventId", log.EventId);
            severity = provider.CreateParameter("@severity", log.Severity.ToString());
            timeStamp = provider.CreateParameter("@timeStamp", log.TimeStamp);

            categories = provider.CreateParameter("@categories", log.CategoriesString);
            extendedProperties = provider.CreateParameter
                ("@extendedProperties", log.ExtendedPropertiesString);

            DbConnection conn;
            DbCommand cmd;

            using (conn = provider.CreateConnection())
            {

                using (cmd = provider.CreateCommand())
                {

                    cmd.Connection = conn;
                    cmd.CommandText = procedureName;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(message);
                    cmd.Parameters.Add(priority);
                    cmd.Parameters.Add(title);
                    cmd.Parameters.Add(eventId);
                    cmd.Parameters.Add(severity);
                    cmd.Parameters.Add(machineName);
                    cmd.Parameters.Add(timeStamp);
                    cmd.Parameters.Add(appDomainName);
                    cmd.Parameters.Add(processId);
                    cmd.Parameters.Add(processName);
                    cmd.Parameters.Add(managedThreadName);
                    cmd.Parameters.Add(categories);
                    cmd.Parameters.Add(extendedProperties);
                    cmd.Parameters.Add(id);

                    conn.Open();
                    cmd.ExecuteNonQuery();

                }

            }

        }
    }
}
